from utils.convert import rj_imglist


def gen_upload_dict(proinfo: dict) -> dict:
    """生成上传字典

    Args:
        proinfo (dict): 产品信息
    """

    # 生成 sku
    _res_sku = []
    _temp_sku = proinfo["skuInfos"]
    for sku in _temp_sku:
        _spec_list = sku["spec"].split("/")
        if len(_spec_list) == 1:
            _unit = "包"
        else:
            _unit = _spec_list[-1]
        _res_sku.append(
            {
                "code": proinfo["code"] + "-" + str(sku["index"]),
                "price": sku["price"],
                "directoryPrice": sku["price"] * 1.9,
                "stock": 999,
                "carryFeeTemplateId": "4611",
                # "specification": {
                #     "packingSpecificationValue": 400,
                #     "packingSpecificationUnit": "包",
                #     "minSpecificationValue": 1,
                #     "minSpecificationUnit": "箱",
                # },
                "skuAttributes": [
                    {
                        "attributeName": "产品规格",
                        "attributeValue": sku["spec"],
                    },
                ],
                "unit": _unit,
            }
        )
    proinfo["skuInfos"] = _res_sku

    proinfo["status"] = 3
    proinfo["photos"] = rj_imglist(proinfo["photos"])  # 生成图片列表
    proinfo["brandId"] = 1542  # 品牌ID
    proinfo["deliveryTime"] = 7  # 货期
    proinfo["categoryId"] = 529  # 分类ID
    return proinfo


def price_update(proinfo: dict) -> dict:
    """更新价格

    Args:
        proinfo (dict): 产品信息
    """
    _res_sku = []
    _temp_sku = proinfo["skuInfos"]
    for sku in _temp_sku:
        _res_sku.append(
            {
                "code": proinfo["code"],
                "sku_code": proinfo["code"] + "-" + str(sku["index"]),
                "price": sku["price"],
                "spec": sku["spec"],
            }
        )
    return _res_sku
